package com.emagic.manage.common.ui.base;

import android.content.Context;
import android.content.res.TypedArray;
import android.os.Bundle;
import android.support.annotation.StringRes;
import android.support.design.widget.CoordinatorLayout;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.TextView;

import com.emagic.manage.R;
import com.emagic.manage.data.exception.BizException;
import com.emagic.manage.data.exception.ServerError;
import com.xitaiinfo.library.compat.errorview.ErrorView;
import com.xitaiinfo.library.compat.view.CircleProgressView;
import com.xitaiinfo.library.compat.widget.BetterViewAnimator;

import java.net.UnknownHostException;

import retrofit2.adapter.rxjava.HttpException;

/**
 * Created by SH on 2016-12-05.
 */

public class WebViewToolBarActivity extends AppStateActivity{
    public Toolbar mToolbar;

    /*base view*/
    private BetterViewAnimator mViewAnimator;
    private FrameLayout mContentView;
    private CoordinatorLayout mBaseView;
    private CircleProgressView mLoadingView;
    private ErrorView mErrorView;

    private TextView mToolbarTitle;

    private TextView mClosedImage,mBackImage;

    private LayoutInflater mInflater;

    private static int[] ATTRS = {
            R.attr.windowActionBarOverlay,
            R.attr.actionBarSize
    };


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mInflater = LayoutInflater.from(this);
        initContentView();
        initToolBar();

    }

    private void initToolBar() {
        /*通过inflater获取toolbar的布局文件*/
        View toolbar = mInflater.inflate(R.layout.toolbar1, mBaseView);
        mToolbar = (Toolbar) toolbar.findViewById(R.id.id_tool_bar);
        mToolbarTitle = (TextView) toolbar.findViewById(R.id.tool_bar_title);
        mClosedImage = (TextView) toolbar.findViewById(R.id.closed_img);
        mBackImage = (TextView) toolbar.findViewById(R.id.back_img);
    }

    private void initContentView() {
        mBaseView = (CoordinatorLayout) mInflater.inflate(R.layout.activity_base, null);
        mViewAnimator = (BetterViewAnimator) mBaseView.findViewById(R.id.view_animator);
        mContentView = (FrameLayout) mBaseView.findViewById(R.id.content_view);
        mLoadingView = (CircleProgressView) mBaseView.findViewById(R.id.loading_view);
        mErrorView = (ErrorView) mBaseView.findViewById(R.id.error_view);
    }

    @SuppressWarnings("ResourceType")
    private void initChildView(int layoutResID) {
        View mChildView = mInflater.inflate(layoutResID, null);
        CoordinatorLayout.LayoutParams params = new CoordinatorLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        TypedArray typedArray = getContext().getTheme().obtainStyledAttributes(ATTRS);
        //获取主题中定义的悬浮标志
        boolean overly = typedArray.getBoolean(0, false);
        //获取主题中定义的toolbar的高度
        int toolBarSize = (int) typedArray.getDimension(1, (int) getContext().getResources().getDimension(R.dimen.abc_action_bar_default_height_material));
        typedArray.recycle();
        //如果是悬浮状态，则不需要设置间距
//        params.topMargin = overly ? 0 : toolBarSize;
        mContentView.addView(mChildView);
        mViewAnimator.setLayoutParams(params);

    }

    public Toolbar getToolbar() {
        return mToolbar;
    }
    public TextView getmClosedImage(){
        return mClosedImage;
    }
    public TextView getmBackImage(){
        return mBackImage;
    }

    public void setToolbarTitle(String title) {
        this.mToolbarTitle.setText(title);
    }

    public void setToolbarTitle(@StringRes int res) {
        this.mToolbarTitle.setText(res);
    }

    @Override
    public void setContentView(int layoutResID) {
        //初始化子类布局
        initChildView(layoutResID);
        //设置当前布局
        setContentView(mBaseView);
        mToolbar.setTitle("");
        //设置toolbar到Activity
        setSupportActionBar(mToolbar);

    }

    @Override
    public void showLoadingView() {
        mViewAnimator.setDisplayedChildId(R.id.loading_view);
    }

    @Override
    public void showEmptyView(ErrorView.Config config, ErrorView.OnRetryListener onRetryListener) {

        mErrorView.setOnRetryListener(onRetryListener);

        mErrorView.setTitle("暂无数据");
        mErrorView.setRetryButtonText("刷新页面");
        mErrorView.setImage(R.mipmap.empty_image);

        if(config != null){
            mErrorView.setConfig(config);
        }
        mViewAnimator.setDisplayedChildId(R.id.error_view);
    }

    @Override
    public void showErrorView(Throwable throwable, ErrorView.Config config, ErrorView.OnRetryListener onRetryListener) {

        if(throwable != null && throwable instanceof BizException){
            //显示错误文本
            showError(((BizException) throwable).message());
        }else if(throwable != null && throwable instanceof UnknownHostException){
            //无网络异常 显示无网络错误
            mErrorView.setTitle("网络请求失败");
            mErrorView.setSubtitle("请检查您的手机是否联网");
            mErrorView.setImage(R.mipmap.ic_network_unavailable);

            mErrorView.setOnRetryListener(onRetryListener);
            //show error view
            mViewAnimator.setDisplayedChildId(R.id.error_view);

        }else {
            //其它错误
            if(throwable != null && throwable instanceof HttpException){
                showError(((HttpException)throwable).message());
            }else if(throwable != null && throwable instanceof ServerError){//服务端异常
                showError("服务端异常!!!");
            }
            mErrorView.setImage(R.mipmap.error_image_server);

            if(config != null){
                mErrorView.setConfig(config);
            }

            mErrorView.setOnRetryListener(onRetryListener);
            //show error view
            mViewAnimator.setDisplayedChildId(R.id.error_view);
        }

    }

    @Override
    public void onLoadingComplete() {
        mViewAnimator.setDisplayedChildId(R.id.content_view);
    }

    @Override
    public Context getContext() {
        return WebViewToolBarActivity.this;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        switch (item.getItemId()){
            case android.R.id.home:
                this.finish();
                break;
        }
        return super.onOptionsItemSelected(item);
    }
}
